<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:WorkProcess:add']"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="WorkProcessList">
      <el-table-column label="环节序号" align="center" prop="orderid" />
      <el-table-column label="流程名称" align="center" prop="flowname" />
      <el-table-column label="关键节点" align="center" prop="flownode" />
      <el-table-column label="计划时间" align="center" prop="starttime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.starttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际时间" align="center" prop="finishtime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应交金额" align="center" prop="planmoney" />
      <el-table-column label="实交金额" align="center" prop="factmoney" />
      <el-table-column label="完成状态" align="center" prop="status" />
      <el-table-column label="操作人" align="center" prop="userid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:WorkProcess:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:WorkProcess:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row style="margin-top: 10px;">
      <el-col :span="1" :offset="23">
        <el-button
          type="primary"
          @click="finish"
          size="mini"
        >完 成</el-button>
      </el-col>
    </el-row>

    <!-- 添加或修改办理进度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程名称" prop="flowname">
          <el-input v-model="form.flowname" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="关键节点" prop="flownode">
          <el-select
            v-model="form.flownode"
            placeholder="请选择关键节点"
            style="width: 220px"
          >
            <el-option :label="'--'" :value="'--'" />
            <el-option :label="'签约'" :value="'签约'" />
            <el-option :label="'按揭到款'" :value="'按揭到款'" />
            <el-option :label="'房款已清'" :value="'房款已清'" />
            <el-option :label="'入住'" :value="'入住'" />
            <el-option :label="'已办房产证'" :value="'已办房产证'" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划时间" prop="starttime">
          <el-date-picker
            v-model="form.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应交金额" prop="planmoney">
          <el-input v-model="form.planmoney" placeholder="请输入应交金额" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkProcess, getWorkProcess, delWorkProcess, addWorkProcess, updateWorkProcess } from "@/api/house_sale/WorkProcess";

export default {
  name: "WorkProcess",
  props: ["buyid","hourseid"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 办理进度表格数据
      WorkProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        buyid: null,
        custid: null,
        hourseid: null,
        flowname: null,
        flownode: null,
        starttime: null,
        finishtime: null,
        planmoney: null,
        factmoney: null,
        orderid: null,
        status: null,
        userid: null,
        oprtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flowname: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        flownode: [
          { required: true, message: "关键节点不能为空", trigger: "blur" }
        ],
        starttime: [
          { required: true, message: "应该完成时间不能为空", trigger: "blur" }
        ],
        planmoney: [
          { required: true, message: "应交金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.buyid = this.buyid;
    this.queryParams.hourseid = this.hourseid;
    this.getList();
  },
  watch: {
    buyid() {
      this.queryParams.buyid = this.buyid;
      this.queryParams.hourseid = null;
      this.getList();
    },
    hourseid() {
      this.queryParams.hourseid = this.hourseid;
      this.queryParams.buyid = null;
      this.getList();
    }
  },
  methods: {
    /** 查询购房流程列表 */
    getList() {
      this.loading = true;
      listWorkProcess(this.queryParams).then(response => {
        this.WorkProcessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processid: null,
        buyid: null,
        custid: null,
        hourseid: null,
        flowname: null,
        flownode: null,
        starttime: null,
        finishtime: null,
        planmoney: null,
        factmoney: null,
        orderid: null,
        status: null,
        userid: null,
        oprtime: null
      };
      this.resetForm("form");
    },
    // 完成按钮操作
    finish() {
      this.$emit("next");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购房流程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processid = row.processid || this.ids
      getWorkProcess(processid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改办理进度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.processid != null) {
            updateWorkProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.orderid = this.WorkProcessList.length + 1;
            this.form.buyid = this.WorkProcessList[0].buyid;
            this.form.custid = this.WorkProcessList[0].custid;
            this.form.hourseid = this.WorkProcessList[0].hourseid;
            this.form.status = '未完成';
            addWorkProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processids = row.processid || this.ids;
      this.$modal.confirm('是否确认删除办理进度编号为"' + processids + '"的数据项？').then(function() {
        return delWorkProcess(processids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
